package com.tinyseed.emcp.crawler.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tinyseed.emcp.crawler.service.EnterpriseCrawService;
import com.tinyseed.emcp.crawler.utils.LoginUtils;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

@Service
@Slf4j
public class EnterpriseCrawServiceImpl implements EnterpriseCrawService {


    @Override
    public List<JSONObject> crawlerJaEnterprise() {
        OkHttpClient okHttpClient = LoginUtils.getLoginClient();
        String enterpriseURL ="http://119.3.187.215/Enterprise/GetListWithPager?_search=false&nd=1567079005165&rows=10000&page=1&sidx=&sord=desc";

        Request request = new Request.Builder().url(enterpriseURL).get().build();
        List<JSONObject> list = null;

        try(Response response = okHttpClient.newCall(request).execute()){

            String content = Objects.requireNonNull(response.body()).string();
            JSONObject object = JSON.parseObject(content);
            list = (List<JSONObject>) object.get("rows");
        }catch (IOException e){
            log.error("爬取京安异常数据失败{}",e);
        }
        return list;
    }

    @Override
    public String crawlerJaWorkshop(String enterpriseId) {
        OkHttpClient okHttpClient = LoginUtils.getLoginClient();
        String enterpriseURL ="http://119.3.187.215/Enterprise/Edit/"+ enterpriseId;
        Request request = new Request.Builder().url(enterpriseURL).get().build();
        String content = null;
        AtomicReference<String> value = new AtomicReference<>();
        try(Response response = okHttpClient.newCall(request).execute()){
            content = Objects.requireNonNull(response.body()).string();
            Document parse = Jsoup.parse(content);
            Element e= parse.getElementById("Allworkshopinfo");
            Optional.ofNullable(e).ifPresent(item->{
                Attributes a = item.attributes();
                String rawValue = a.get("value");
                value.set(rawValue);
            });
        }catch (IOException e){
            log.error("爬取京安异常数据失败{}",e);
        }
        return value.get();
    }

    @Override
    public  Map<String,String> crawlerJaMonitorHtml(String monitorId) {
        OkHttpClient okHttpClient = LoginUtils.getLoginClient();
        String enterpriseURL ="http://119.3.187.215/Monitor/Edit/"+ monitorId;
        Request request = new Request.Builder().url(enterpriseURL).get().build();
        String content = null;

        Map<String,String> hashmap = new HashMap<>();

        AtomicReference<String> value = new AtomicReference<>();
        try(Response response = okHttpClient.newCall(request).execute()){
            content = Objects.requireNonNull(response.body()).string();
            Document parse = Jsoup.parse(content);
            Element shop= parse.getElementById("AllShopId");
            Element meter= parse.getElementById("MeterId");
            Element swicthID= parse.getElementById("SwicthID");
            Optional.ofNullable(shop).ifPresent(item->{
                Attributes a = item.attributes();
                String rawValue = a.get("value");
                hashmap.put("shopid",rawValue);
            });
            Optional.ofNullable(meter).ifPresent(item->{
                Attributes a = item.attributes();
                String rawValue = a.get("value");
                hashmap.put("meter",rawValue);
            });
            Optional.ofNullable(swicthID).ifPresent(item->{
                Attributes a = item.attributes();
                String rawValue = a.get("value");
                hashmap.put("swicthID",rawValue);
            });





        }catch (IOException e){
            log.error("爬取京安异常数据失败{}",e);
        }
        return hashmap;
    }



    @Override
    public List<JSONObject> crawlerJAMonitor(){
        OkHttpClient okHttpClient = LoginUtils.getLoginClient();

        List<JSONObject> list = new ArrayList<>();

        for(int i =1 ;i<=5;i++){
            String enterpriseURL ="http://119.3.187.215/Monitor/GetListWithPager?_search=false&nd=1587379995343&rows=2000&page="+i+"&sidx=&sord=desc";
            Request request = new Request.Builder().url(enterpriseURL).get().build();
            try(Response response = okHttpClient.newCall(request).execute()){

                String content = Objects.requireNonNull(response.body()).string();
                JSONObject object = JSON.parseObject(content);
                List tempList = (List<JSONObject>) object.get("rows");
                list.addAll(tempList);
            }catch (IOException e){
                log.error("爬取京安异常数据失败{}",e);
            }
        }
        return list;
    }

}
